package cn.com.member.controller.match;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_enums;
import cn.com.api.entity.ApiU_match;
import cn.com.api.entity.ApiU_match_prize;
import cn.com.api.service.ApiT_enumsService;
import cn.com.api.service.ApiU_matchService;
import cn.com.api.service.ApiU_match_prizeService;
import cn.com.api.service.ApiU_match_voteService;
import cn.com.api.service.ApiU_match_worksService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_VIEWS + "/")
public class U_matchController {

	private static Logger logger = Logger.getLogger(U_matchController.class);

	@Autowired
	private ApiT_enumsService apiT_enumsService;

	@Autowired
	private ApiU_matchService apiU_matchService;

	@Autowired
	private ApiU_match_prizeService apiU_match_prizeService;

	@Autowired
	private ApiU_match_worksService apiU_match_worksService;

	@Autowired
	private ApiU_match_voteService apiU_match_voteService;

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_match-info-add")
	public void u_match_info_add(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String match_id = request.getParameter("match_id");
			String match_name = request.getParameter("match_name");
			String join_count = request.getParameter("join_count");
			String ent_id = request.getParameter("ent_id");
			String amount = request.getParameter("amount");
			String match_introduce = request.getParameter("match_introduce");
			String match_rules = request.getParameter("match_rules");
			String thumbnail = request.getParameter("thumbnail");
			String thumbnail_h5 = request.getParameter("thumbnail_h5");
			String pstart_time = request.getParameter("pstart_time");
			String pend_time = request.getParameter("pend_time");
			String vstart_time = request.getParameter("vstart_time");
			String vend_time = request.getParameter("vend_time");
			String result_time = request.getParameter("result_time");
			String counter_fee = request.getParameter("counter_fee");
			String bonus_amount = request.getParameter("bonus_amount");
			String status = request.getParameter("status");
			String reject_reason = request.getParameter("reject_reason");
			String is_pay = request.getParameter("is_pay");
			String win_amount = request.getParameter("win_amount");
			String guess_amount = request.getParameter("guess_amount");
			String is_need_vedio = request.getParameter("is_need_vedio");
			String ext1 = request.getParameter("ext1");
			String ext2 = request.getParameter("ext2");
			String ext3 = request.getParameter("ext3");
			String ext4 = request.getParameter("ext4");
			String ext5 = request.getParameter("ext5");
			String audit_by = request.getParameter("audit_by");
			String match_cate = request.getParameter("match_cate");
			String id = request.getParameter("id");
			SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String audit_time = sd.format(new Date());

			String[] prize_name = request.getParameterValues("prize_name");
			String[] prize_num = request.getParameterValues("prize_num");
			String[] winner_count = request.getParameterValues("winner_count");
			String[] prize_money = request.getParameterValues("prize_money");
			String[] prize_percent = request.getParameterValues("prize_percent");
			String[] prize_remark = request.getParameterValues("remark");
			List<ApiU_match_prize> list_prize = new ArrayList<ApiU_match_prize>();
			ApiU_match_prize match_prize = new ApiU_match_prize();
			if (prize_name != null && prize_name.length > 0) {
				for (int i = 0; i < prize_remark.length; i++) {
					match_prize = new ApiU_match_prize();
					match_prize.setMatch_id(match_id);
					match_prize.setPrize_money(Integer.parseInt(prize_money[i]));
					match_prize.setPrize_name(prize_name[i]);
					match_prize.setPrize_num(Integer.parseInt(prize_num[i]));
					match_prize.setPrize_percent(Integer.parseInt(prize_percent[i]));
					match_prize.setWinner_count(Integer.parseInt(winner_count[i]));
					match_prize.setRemark("");
					list_prize.add(match_prize);
				}
			}

			if (join_count == null) {
				join_count = "0";
			}

			if (amount == null) {
				amount = "0";
			}

			if (counter_fee == null) {
				counter_fee = "0";
			}

			if (bonus_amount == null) {
				bonus_amount = "0";
			}

			if (win_amount == null) {
				win_amount = "0";
			}

			if (guess_amount == null) {
				guess_amount = "0";
			}

			if (is_need_vedio == null) {
				is_need_vedio = "0";
			}
			if (is_pay == null) {
				is_pay = "0";
			}
			// add
			if (id == null || (id.trim()).equals("")) {
				ApiU_match u_match = new ApiU_match();
				u_match.setMatch_name(match_name);
				u_match.setJoin_count(Integer.parseInt(join_count));
				u_match.setEnt_id("");
				u_match.setAmount(new BigDecimal(amount));
				u_match.setMatch_introduce(match_introduce);
				u_match.setMatch_rules(match_rules);
				u_match.setThumbnail(thumbnail);
				u_match.setThumbnail_h5(thumbnail_h5);
				u_match.setPstart_time(pstart_time);
				u_match.setPend_time(pend_time);
				u_match.setVstart_time(vstart_time);
				u_match.setVend_time(vend_time);
				u_match.setResult_time(result_time);
				u_match.setCounter_fee(new BigDecimal(counter_fee));
				u_match.setBonus_amount(new BigDecimal(bonus_amount));
				u_match.setStatus(0);
				u_match.setReject_reason("");
				u_match.setIs_pay(Integer.parseInt(is_pay));
				u_match.setWin_amount(new BigDecimal(win_amount));
				u_match.setGuess_amount(new BigDecimal(guess_amount));
				u_match.setIs_need_vedio(Integer.parseInt(is_need_vedio));
				u_match.setExt1("");
				u_match.setExt2("");
				u_match.setExt3("");
				u_match.setExt4("");
				u_match.setExt5("");
				u_match.setAudit_by("");
				u_match.setAudit_time("");
				u_match.setMatch_cate(match_cate);
				apiU_matchService.U_matchInfoInsertion(u_match, list_prize);
			}
			// edit
			else {
				ApiU_match u_match = apiU_matchService.findU_matchById(Integer.valueOf(id));

				u_match.setMatch_id(Integer.parseInt(match_id));
				u_match.setMatch_name(match_name);
				u_match.setJoin_count(Integer.parseInt(join_count));
				u_match.setEnt_id("");
				u_match.setAmount(new BigDecimal(amount));
				u_match.setMatch_introduce(match_introduce);
				u_match.setMatch_rules(match_rules);
				u_match.setThumbnail(thumbnail);
				u_match.setThumbnail_h5(thumbnail_h5);
				u_match.setPstart_time(pstart_time);
				u_match.setPend_time(pend_time);
				u_match.setVstart_time(vstart_time);
				u_match.setVend_time(vend_time);
				u_match.setResult_time(result_time);
				u_match.setCounter_fee(new BigDecimal(counter_fee));
				u_match.setBonus_amount(new BigDecimal(bonus_amount));
				u_match.setStatus(0);
				u_match.setReject_reason("");
				u_match.setIs_pay(Integer.parseInt(is_pay));
				u_match.setWin_amount(new BigDecimal(win_amount));
				u_match.setGuess_amount(new BigDecimal(guess_amount));
				u_match.setIs_need_vedio(Integer.parseInt(is_need_vedio));
				u_match.setExt1("");
				u_match.setExt2("");
				u_match.setExt3("");
				u_match.setExt4("");
				u_match.setExt5("");
				u_match.setAudit_by("");
				u_match.setAudit_time("");
				u_match.setMatch_cate(match_cate);
				apiU_matchService.updateU_match_info(u_match, list_prize);
			}
			//
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "添加成功！");

		} catch (Exception e) {
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "添加失败！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 获取赛事列表
	 */
	@RequestMapping(value = "u_match-info-list")
	public ModelAndView u_match_list(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mv = new ModelAndView("/match/u_match_list");
		try {

			String match_cate = request.getParameter("match_cate");
			String status = request.getParameter("status");
			Map<String, Object> param = new HashMap<String, Object>();

			if (match_cate != null && !"-1".equals(match_cate)) {
				param.put("match_cate", match_cate);
				mv.addObject("match_cate", match_cate);
			}

			if (status != null && !"-1".equals(status)) {
				param.put("status", status);
			}
			List<ApiT_enums> cate = apiT_enumsService.findT_enumssByItem_key("match_cates_list");
			List<ApiT_enums> cate_list = apiT_enumsService.findT_enumssByParent_id(cate.get(0).getId() + "");
			mv.addObject("match_cates_list", cate_list);

			List<ApiU_match> u_matchs = apiU_matchService.findU_matchs_condition(param);
			mv.addObject("u_matchs", u_matchs);
			mv.addObject("match_cate", match_cate);
			mv.addObject("status", status);
		} catch (Exception e) {
			logger.error("===========[U_matchController[u_match_list[获取赛事列表失败]]]========", e);
		}
		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_match-add-info-pre")
	public ModelAndView u_match_add_info_pre(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("/match/u_match_add");
		try {

			List<ApiT_enums> cate = apiT_enumsService.findT_enumssByItem_key("match_cates_list");
			List<ApiT_enums> cate_list = apiT_enumsService.findT_enumssByParent_id(cate.get(0).getId() + "");
			mv.addObject("match_cates_list", cate_list);
			if (request.getParameter("id") != null) {
				String id = request.getParameter("id");
				ApiU_match u_match = apiU_matchService.findU_matchById(Integer.valueOf(id));
				List<ApiU_match_prize> list_match_prize = apiU_match_prizeService.findU_match_prizesByMatchId(id);

				mv.addObject("u_match", u_match);
				mv.addObject("list_match_prize", list_match_prize);
			}
		} catch (Exception e) {
			logger.error("===========[U_matchController[u_match_add_info_pre[获取编辑信息失败]]]========", e);
		}
		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_match-detail-info-pre")
	public ModelAndView u_match_detail_info_pre(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("/match/u_match_detail");
		try {
			List<ApiT_enums> cate = apiT_enumsService.findT_enumssByItem_key("match_cates_list");
			List<ApiT_enums> cate_list = apiT_enumsService.findT_enumssByParent_id(cate.get(0).getId() + "");
			mv.addObject("match_cates_list", cate_list);
			if (request.getParameter("id") != null) {
				String id = request.getParameter("id");
				ApiU_match u_match = apiU_matchService.findU_matchById(Integer.valueOf(id));
				List<ApiU_match_prize> list_match_prize = apiU_match_prizeService.findU_match_prizesByMatchId(id);

				mv.addObject("u_match", u_match);
				mv.addObject("list_match_prize", list_match_prize);
			}
		} catch (Exception e) {
			logger.error("===========[U_matchController[u_match_detail_info_pre[获取详情信息失败]]]========", e);
		}
		return mv;
	}

	/**
	 * 获取赛事列表
	 */
	@RequestMapping(value = "u_match-enroll-info-list")
	public ModelAndView u_match_enroll_list(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mv = new ModelAndView("/match/u_match_enroll_list");
		try {

			String match_cate = request.getParameter("match_cate");
			String status = request.getParameter("status");
			Map<String, Object> param = new HashMap<String, Object>();
			List<ApiT_enums> cate = apiT_enumsService.findT_enumssByItem_key("match_cates_list");
			List<ApiT_enums> cate_list = apiT_enumsService.findT_enumssByParent_id(cate.get(0).getId() + "");
			mv.addObject("match_cates_list", cate_list);

			if (match_cate != null && !"-1".equals(match_cate)) {
				param.put("match_cate", match_cate);
			}

			if (status != null && !"-1".equals(status)) {
				param.put("status", status);
			}
			param.put("type", "1");// 1：报名中
			List<ApiU_match> u_matchs = apiU_matchService.findU_matchs_enroll_condition(param);
			mv.addObject("u_matchs", u_matchs);
			mv.addObject("match_cate", match_cate);
			mv.addObject("status", status);
		} catch (Exception e) {
			logger.error("===========[U_matchController[u_match_list[获取赛事列表失败]]]========", e);
		}
		return mv;
	}

	/**
	 * 获取投票中赛事列表
	 */
	@RequestMapping(value = "u_match-vote-info-list")
	public ModelAndView u_match_vote_list(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mv = new ModelAndView("/match/u_match_vote_list");
		try {

			String match_cate = request.getParameter("match_cate");
			String status = request.getParameter("status");
			Map<String, Object> param = new HashMap<String, Object>();
			List<ApiT_enums> cate = apiT_enumsService.findT_enumssByItem_key("match_cates_list");
			List<ApiT_enums> cate_list = apiT_enumsService.findT_enumssByParent_id(cate.get(0).getId() + "");
			mv.addObject("match_cates_list", cate_list);

			if (match_cate != null && !"-1".equals(match_cate)) {
				param.put("match_cate", match_cate);
			}

			if (status != null && !"-1".equals(status)) {
				param.put("status", status);
			}
			param.put("type", "2");// 2：投票中
			List<ApiU_match> u_matchs = apiU_matchService.findU_matchs_vote_condition(param);
			mv.addObject("u_matchs", u_matchs);
			mv.addObject("match_cate", match_cate);
			mv.addObject("status", status);
		} catch (Exception e) {
			logger.error("===========[U_matchController[u_match_vote_list[获取赛事投票中列表失败]]]========", e);
		}
		return mv;
	}

	/**
	 * 获取投票中赛事列表
	 */
	@RequestMapping(value = "u_match-end-info-list")
	public ModelAndView u_match_end_list(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mv = new ModelAndView("/match/u_match_end_list");
		try {

			String match_cate = request.getParameter("match_cate");
			String status = request.getParameter("status");
			Map<String, Object> param = new HashMap<String, Object>();
			List<ApiT_enums> cate = apiT_enumsService.findT_enumssByItem_key("match_cates_list");
			List<ApiT_enums> cate_list = apiT_enumsService.findT_enumssByParent_id(cate.get(0).getId() + "");
			mv.addObject("match_cates_list", cate_list);

			if (match_cate != null && !"-1".equals(match_cate)) {
				param.put("match_cate", match_cate);
			}

			if (status != null && !"-1".equals(status)) {
				param.put("status", status);
			}
			param.put("type", "3");// 3：已结束
			List<ApiU_match> u_matchs = apiU_matchService.findU_matchs_enroll_condition(param);
			mv.addObject("u_matchs", u_matchs);
			mv.addObject("match_cate", match_cate);
			mv.addObject("status", status);
		} catch (Exception e) {
			logger.error("===========[U_matchController[u_match_vote_list[获取赛事列表失败]]]========", e);
		}
		return mv;
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */

	@RequestMapping(value = "u_match-delete")
	public void u_match_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiU_matchService.deleteU_match(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
